import re

import requests
import pymysql
import threading
import random
import xlrd

list3 = []


ua=[
"Mozilla/5.0 (Windows NT 6.2;en-US) AppleWebKit/537.32.36 (KHTML, live Gecko) Chrome/59.0.3047.101 Safari/537.32",

    "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.117 Safari/537.36",

    "Mozilla/5.0 (iPhone; CPU iPhone OS 14_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 MicroMessenger/8.0.5(0x18000528) NetType/4G Language/zh_CN",

    "Mozilla/5.0 (Linux; Android 11; M2012K11AC Build/RKQ1.200826.002; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/77.0.3865.120 MQQBrowser/6.2 TBS/045613 Mobile Safari/537.36 MMWEBID/7803 MicroMessenger/8.0.3.1880(0x2800033D) Process/tools WeChat/arm64 Weixin NetType/WIFI Language/zh_CN ABI/arm64",

    "Mozilla/5.0 (Linux; Android 9; Mi Note 3 Build/PKQ1.181007.001; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/77.0.3865.120 MQQBrowser/6.2 TBS/045609 Mobile Safari/537.36 MMWEBID/4799 MicroMessenger/8.0.2.1860(0x2800023D) Process/tools WeChat/arm64 Weixin NetType/4G Language/zh_CN ABI/arm64",

    "Mozilla/5.0 (iPhone; CPU iPhone OS 14_5_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 MicroMessenger/8.0.5(0x18000528) NetType/4G Language/zh_CN",

    "Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)",

    "Mozilla/5.0 (Linux; Android 11; V1824A Build/RP1A.200720.012; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/77.0.3865.120 MQQBrowser/6.2 TBS/045611 Mobile Safari/537.36 MMWEBID/8305 MicroMessenger/8.0.2.1860(0x2800023D) Process/tools WeChat/arm64 Weixin NetType/WIFI Language/zh_CN ABI/arm64",

    "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36",

    "Mozilla/5.0 (iPhone; CPU iPhone OS 14_5_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 MicroMessenger/8.0.3(0x1800032c) NetType/WIFI Language/zh_CN",

    "Mozilla/5.0 (Linux; U; Android 10; zh-cn; MI 9 Build/QKQ1.190825.002) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/71.0.3578.141 Mobile Safari/537.36 XiaoMi/MiuiBrowser/13.3.12",
]




def jim(nu, cursor, conn, coon, count):
    url = 'https://adv-listing.eccang.com/images/image/list/page/1/pageSize/20'
    number = random.randint(0, 10)
    headers = {
        'cookie': 'image_category_is_show=1; currentPage=0%7B%7C%7D%2Fsystem%2Fhome%7B%7C%7D%E9%A6%96%E9%A1%B5; UM_distinctid=17c15b16b7275-02b55430879ff9-2343360-1fa400-17c15b16b7376d; _ga=GA1.2.368327117.1632464693; SID_a94cdd47df94bdf79824f206af938ffc=ad22116494ec696b69b5b2891aa98182; 15706_dashboard_list=%7B%22user-profile-wg%22%3A1%2C%22publish-version-wg%22%3A2%2C%22platform-item-listing-wg%22%3A3%7D; 15706_bulletin=%7B%22is_tip%22%3Atrue%2C%22data%22%3A%7B%22v_id%22%3A%22274%22%2C%22v_tip_content%22%3A%22%E4%BA%B2%E7%88%B1%E7%9A%84%E7%94%A8%E6%88%B7%EF%BC%8C%E4%B8%BA%E4%BA%86%E7%BB%99%E5%90%84%E4%BD%8D%E5%AE%A2%E6%88%B7%E6%8F%90%E4%BE%9B%E6%9B%B4%E5%8A%A0%E7%A8%B3%E5%AE%9A%E9%AB%98%E6%95%88%E7%9A%84%E7%BF%BB%E8%AF%91%E6%9C%8D%E5%8A%A1%EF%BC%8C%E6%88%91%E4%BB%AC%E5%86%B3%E5%AE%9A%E4%BA%8E%2012%E6%9C%8821%E6%97%A5%20%E8%B5%B7%E5%AF%B9%20%5C%22%E4%B8%80%E9%94%AE%E7%BF%BB%E8%AF%91%E2%80%9D%E6%8E%A5%E5%8F%A3%E8%BF%9B%E8%A1%8C%E5%8D%87%E7%BA%A7%EF%BC%8C%E6%8E%A5%E5%8F%A3%E5%8D%87%E7%BA%A7%E6%9C%9F%E9%97%B4%EF%BC%8C%E2%80%9C%E4%B8%80%E9%94%AE%E7%BF%BB%E8%AF%91%E2%80%9D%E6%9C%8D%E5%8A%A1%E5%B0%86%E6%9A%82%E5%81%9C%E4%BD%BF%E7%94%A8%EF%BC%8C%E6%81%A2%E5%A4%8D%E6%97%B6%E9%97%B4%E5%B0%86%E5%8F%A6%E8%A1%8C%E9%80%9A%E7%9F%A5%E3%80%82%E5%A6%82%E6%9C%89%E4%BB%BB%E4%BD%95%E4%B8%8D%E4%BE%BF%E4%B9%8B%E5%A4%84%EF%BC%8C%E6%95%AC%E8%AF%B7%E8%B0%85%E8%A7%A3%EF%BC%81%22%2C%22v_tip_start_date%22%3A%222020-12-18%2000%3A00%3A00%22%2C%22v_tip_end_date%22%3A%222020-12-27%2000%3A00%3A00%22%7D%2C%22id%22%3A%22274%232020-12-18%2000%3A00%3A00%232020-12-27%2000%3A00%3A00%22%7D; LANGUAGE=zh_CN; Hm_lvt_df17a76b1cc89f0e7c4dcc7df476cb95=1632464854,1632465183,1632617901,1632703828; Hm_lpvt_df17a76b1cc89f0e7c4dcc7df476cb95=1632703833; SERVERID=b83abf07b6b0dfc976c0493b5f0a6b3b|1632703844|1632621613',
        'Referer': 'https://adv-listing.eccang.com/images/image/list?menu=imageCenter',
        'User-Agent': '{}'.format(ua[number]),

    }
    # print(ua[number])
    data = {
        'E1': '',
        'type': '4',
        'code': '{}'.format(nu),
        'E12': '1',
        'search_sort[1]:': ''
    }

    res = requests.post(url, headers=headers, data=data,verify=False).json()
    print(res)
    print(nu, '*' * 3, count, '*' * 10, res["total"])
    if res["total"] != '0':
        url_1 = 'https://adv-listing.eccang.com/images/image/list/page/1/pageSize/{}'.format(res["total"])
        res_1 = requests.post(url=url_1, headers=headers, data=data,verify=False).json()
        num = 0
        for i in res_1["data"]:
            num += 1
            # picture图片名称：
            picture = i["E2"] + '.jpg'
            # 图片链接
            picture_link = i["E3"]
            # 图片代码前缀
            picture_prefix = nu
            print(num, '*' * 20)
            print(picture)
            print(picture_link)
            print(picture_prefix)
            sql = 'insert into yicang_publish_copy1(picture, picture_link, picture_prefix) values(%s, %s, %s)'

            cursor.execute(sql, (picture, picture_link, picture_prefix))

            conn.commit()
    else:
        coon.write(f'{nu}\n')


def run():
    count = 0
    list_ts = []
    coon = open('no主.xlsx', 'w', encoding='utf-8')
    coon1 = open('wenti主.xlsx', 'w', encoding='utf-8')
    conn = pymysql.connect(host='192.168.2.205', port=3306, user='jinhuitao', password='jinhuitao_123456',
                           database='sunway_db', charset='utf8')
    cursor = conn.cursor()
    for nu in list3:
        count+=1
        try:
            for i in range(10):
                jim(nu, cursor, conn, coon, count)
                break
        except Exception as g:
            coon1.write(nu)
            print('{}有问题'.format(nu))


    #     ts = threading.Thread(target=jim, args=(nu, cursor, conn,))
    #     list_ts.append(ts)
    #
    # for i_ts1 in list_ts:
    #     i_ts1.start()  # 启动线程
    #
    # for i_ts in list_ts:
    #     i_ts.join()  # 关闭线程


def guolv():
    workbook = xlrd.open_workbook(r'D:/商品款号.xlsx')
    sheet = workbook.sheet_by_name('Sheet1')
    for i in range(1, 27293):  # 27293
        names00 = sheet.cell(i, 0)
        name00 = re.findall("text:'(.*?)'", str(names00))
        if name00 == []:
            name00 = 'none'
        else:
            name00 = re.findall("text:'(.*?)'", str(names00))[0]
        print(name00)
        list3.append(name00)
    # return list3


guolv()

run()
